from time import sleep

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from Objects.LoginPage import LoginPage
from Until.ParseConfigFile import ParserConfigFile
from Until.until import find_element, find_elements


class AddCommodityPage:

    def __init__(self,wd):
        self.wd = wd
        self.parse = ParserConfigFile()
        self.sectionLocation = self.parse.getSection('freshMatketAddCommodity')

    def input_searchname(self,name):
        '''输入商品名称'''
        try:
            Addtype,expression = self.sectionLocation['addcommodity.search'].split(':')
            find_element(self.wd,Addtype,expression).send_keys(name)
        except Exception as e:
            raise e

    def click_searchBtn(self):
        '''
        点击搜索
        :return:
        '''
        try:
            Addtype,expression = self.sectionLocation['addcommodity.searchbtn'].split(':')
            find_element(self.wd,Addtype,expression).click()
        except Exception as e:
            raise e

    def commodity_detial(self):
        '''
        进入第一个商品详情
        :return:
        '''
        try:
            Addtype,expression = self.sectionLocation['addcommodity.goods'].split(':')
            find_element(self.wd,Addtype,expression).click()
        except Exception as e:
            raise e

    def switch_windows(self):
        '''
        切换至商品详情页窗体
        :return:
        '''
        try:
            new_windows = wd.window_handles[-1]
            wd.switch_to.window(new_windows)
        except Exception as e:
            raise e

    def Add_nowbuy(self):
        '''
        立即添加购物车
        :return:
        '''
        try:
            Addtype,expression = self.sectionLocation['addcommodity.add'].split(':')
            find_element(self.wd,Addtype,expression).click()
        except Exception as e:
            raise e

    def Jiesuan_btn(self):
        '''
        点击结算按钮
        :return:
        '''
        try:
            Addtype,expression = self.sectionLocation['addcommodity.jiesuan'].split(':')
            find_element(self.wd,Addtype,expression).click()
        except Exception as e:
            raise e

    def flow_btn(self):
        '''
        登录会员中心
        :return:
        '''
        try:
            Addtype,expression = self.sectionLocation['addcommodity.login'].split(':')
            find_element(self.wd,Addtype,expression).click()
        except Exception as e:
            raise e

    def login(self,wd,username,password):
        try:
            loginPage = LoginPage(wd)
            loginPage.input_username(username)
            loginPage.input_password(password)
            loginPage.click_SubmitLogin()
        except Exception as e:
            raise e

    def re_Jiesuanbtn(self):
        '''
        点击结算按钮
        :return:
        '''
        try:
            Addtype, expression = self.sectionLocation['addcommodity.jiesuan'].split(':')
            find_element(self.wd, Addtype, expression).click()
        except Exception as e:
            raise e

    def click_deliveryStyle(self):
        '''
        选择配送方式
        默认0【上门取货】  1 【运费到付】 2【圆通】 3【申通】 4【顺丰】
        :return:
        '''
        try:
            Addtype,expression = self.sectionLocation['addcommodity.Deliverystyle'].split(':')
            find_elements(self.wd,Addtype, expression)[1].click()
        except Exception as e:
            raise e

    def click_PayStyle(self):
        '''
        选择支付方式
        5【支付宝】 6【余额支付】 7【微信】
        :return:
        '''
        try:
            Addtype, expression = self.sectionLocation['addcommodity.PayStyle'].split(':')
            find_element(self.wd, Addtype, expression).click()
        except Exception as e:
            raise e

    def SubmitBtn(self):
        '''
        提交订单
        :return:
        '''
        try:
            Addtype, expression = self.sectionLocation['addcommodity.submit'].split(':')
            find_elements(self.wd, Addtype, expression).click()
        except Exception as e:
            raise e



if __name__ == '__main__':
    chromeDriver_path = 'E:\Python\chromedriver.exe'
    options = Options()
    options.add_argument('--start-maximized')
    wd = webdriver.Chrome(executable_path=chromeDriver_path, options=options)
    wd.get('http://www.cherryzqr.top/fsmarket/index.php')

    add = AddCommodityPage(wd)
    add.input_searchname('牛奶')
    add.click_searchBtn()
    add.commodity_detial()
    sleep(1)
    add.switch_windows()
    add.Add_nowbuy()
    sleep(1)
    add.Jiesuan_btn()
    sleep(1)
    add.flow_btn()
    # 登录
    add.login(wd,'cherry1','123456')
    sleep(5)
    add.re_Jiesuanbtn()
    sleep(3)
    add.click_deliveryStyle()
    sleep(1)
    add.click_PayStyle()
    add.SubmitBtn()